package cn.zifangsky.jvm.part2.reference;

/**
 * 测试对象在 {@link #finalize()} 方法中复活
 *
 * @author zifangsky
 * @date 2020/7/11
 * @since 1.0.0
 */
public class Resurrection {
    public static Resurrection obj;

    public static void main(String[] args) throws InterruptedException {
        obj = new Resurrection();
        obj = null;
        System.gc();

        Thread.sleep(1000);
        if (obj == null) {
            System.out.println("第一次GC后，obj已经为null");
        } else {
            System.out.println("第一次GC后，obj仍然可用");
        }

        System.out.println("********** 第二次GC **********");
        obj = null;
        System.gc();

        Thread.sleep(1000);
        if (obj == null) {
            System.out.println("第二次GC后，obj已经为null");
        } else {
            System.out.println("第二次GC后，obj仍然可用");
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("Resurrection finalize method was called");
        obj = this;
    }

}
